package com.fhz.a2102shopjava.ui.goods;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fhz.a2102shopjava.R;
import com.fhz.a2102shopjava.base.BaseNoViewModelFragment;
import com.fhz.a2102shopjava.callback.ApiResponse;
import com.fhz.a2102shopjava.databinding.FragmentDashboardBinding;
import com.fhz.a2102shopjava.databinding.FragmentGoodsBinding;
import com.fhz.a2102shopjava.entity.Orders;
import com.fhz.a2102shopjava.entity.ShoppingCart;
import com.fhz.a2102shopjava.ui.dashboard.DashboardViewModel;
import com.fhz.a2102shopjava.ui.login.ui.login.LoginActivity;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * 购物车模块
 */
@AndroidEntryPoint
public class GoodsFragment extends BaseNoViewModelFragment<GoodsViewModel, FragmentGoodsBinding> {

    private static final String TAG = "GoodsFragment";
    List<ShoppingCart> mShoppingCartList;
    GoodsItemRecyclerViewAdapter goodsItemRecyclerViewAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_goods;
    }

    @Override
    protected void initData() {

        int userId = MMKV.defaultMMKV().decodeInt("userId", 0);
        if(userId == 0){
            LoginActivity.launch(getActivity());
            Toast.makeText(getActivity(), "请先登录!", Toast.LENGTH_SHORT).show();
            return;
        }

        //编辑按钮
        mBinding.shopCarEdit.setOnClickListener(v ->{
            for (int i = 0; i <mShoppingCartList.size(); i++) {
                goodsItemRecyclerViewAdapter.notifyItemChanged(i,true);
            }
        });

        mShoppingCartList = new ArrayList<>();
        goodsItemRecyclerViewAdapter = new GoodsItemRecyclerViewAdapter(getActivity(), mShoppingCartList);
        mBinding.shopCarRecyclerView.setAdapter(goodsItemRecyclerViewAdapter);
        mBinding.shopCarRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));

        //查询购物车
        mViewModel.getAllByUserId(userId).observe(this, listApiResponse -> {
            mShoppingCartList.addAll(listApiResponse.getData());
            goodsItemRecyclerViewAdapter.notifyDataSetChanged();

            //更新选中的总价
            updateTotalPrice();
        });

//        goodsItemRecyclerViewAdapter.setItemClickListener((view, position) -> {
//
//        });

        goodsItemRecyclerViewAdapter.setItemClickListener(new GoodsItemRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //当前条目
            }

        });

        //子view点击事件
        goodsItemRecyclerViewAdapter.setOnSubItemClickListener((id, position) -> {
            //加法
            if(id == R.id.goods_item_add){
                ShoppingCart shoppingCart = mShoppingCartList.get(position);
                mViewModel.updateNum(shoppingCart.getCarId(),1).observe(this, stringApiResponse -> {
                    //成功!
                    if(stringApiResponse.code == 0){
                        //更新适配器中的值
                        int carNum = stringApiResponse.getData().getCarNum();
                        mShoppingCartList.set(position,stringApiResponse.getData());
                        goodsItemRecyclerViewAdapter.notifyItemChanged(position,carNum);
                        updateTotalPrice();
                    }else{
                        System.out.println(stringApiResponse.msg);
                    }
                });
            //减法
            }else if (id == R.id.goods_item_minus){
                ShoppingCart shoppingCart = mShoppingCartList.get(position);
                if(shoppingCart.getCarNum() <= 1){
                    Toast.makeText(getActivity(), "不能在少了,兄弟.", Toast.LENGTH_SHORT).show();
                    return;
                }
                mViewModel.updateNum(shoppingCart.getCarId(),-1).observe(this, stringApiResponse -> {
                    //成功!
                    if(stringApiResponse.code == 0){
                        //更新适配器中的值
                        int carNum = stringApiResponse.getData().getCarNum();
                        mShoppingCartList.set(position,stringApiResponse.getData());
                        goodsItemRecyclerViewAdapter.notifyItemChanged(position,carNum);

                        updateTotalPrice();
                    }else{
                        System.out.println(stringApiResponse.msg);
                    }
                });
            }
            else if (id == R.id.goods_item_radio) {
                updateTotalPrice();
            }
        });
    }

    /**
     * 更新总价
     */
    private void updateTotalPrice(){
        float total = 0;
        for (int i = 0; i < mShoppingCartList.size(); i++) {
            ShoppingCart shoppingCart = mShoppingCartList.get(i);
            if(!shoppingCart.isCheck()){
                continue;
            }
            total += shoppingCart.getCarNum() * Float.parseFloat(shoppingCart.getCarPrice());
        }
        mBinding.shopCarTotalPrice.setText(String.valueOf(total));
    }

    @Override
    protected GoodsViewModel createViewModel() {
        return mViewModel = new ViewModelProvider(this).get(GoodsViewModel.class);
    }
}